<?php
namespace app\api\controller\service;

use app\api\model\service\SkillEnsurePay;
use app\common\controller\Api;
use addons\service\library\Pay;
use fast\Random;
use think\Db;
/**
 * 首页接口
 */
class Ensure extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 服务员缴纳保证金
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function skillEnsure()
    {
        $uid = $this->auth->id;
        $userInfo = model('app\api\model\service\UserInfo')->where(['user_id'=>$uid])->field('id,is_skill,is_shop')->find();
        $type = input('type','');
        if($type == 'pay')
        {
            $userInfo['is_skill'] != 1 && $this->error('非服务者无法支付保证金');
            $id = input('id/d','');
            $paytype = input('paytype/d',0);
            $price = sprintf("%.2f",input('price',''));
            $orderId = 'SkillEn'.Random::alnum(4).'-'.$uid.'-'.time();
            $re = '';
            Db::startTrans();
            try{
                $skillEnsurePay = new \app\api\model\service\SkillEnsurePay(['user_id'=>$uid,'price'=>$price,'orderId'=>$orderId,'paytype'=>$paytype]);
                $skillEnsurePay->allowField(true)->save();
                if($price > 0)
                {
                    $re = \addons\service\library\Pay::payOrder(['amount'=>$price,'orderid'=>$orderId,'title'=>'充值保证金'],$paytype,$uid,1);
                }else{
                    \app\api\model\service\SkillEnsurePay::where(['id'=>$skillEnsurePay->id])->update(['state'=>1,'paytime'=>time()]);
                    \app\api\model\service\SkillEnsurePay::updateSkillEnsure($uid,$price);
                }
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error('支付拉起失败',$e->getMessage());
            }
            $this->success('支付信息返回成功',$re);
        }
        $list = model('app\api\model\service\SkillEnsure')->field('id,name,price,accept_nums')->order('price asc')->select();
        $this->success('信息返回成功',$list);
    }

    /**
     * 服务人员充值保证金记录
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function skillEnsurePayList()
    {
        $uid = $this->auth->id;
        $page = input('page/d',1);
        $list = model('app\api\model\service\SkillEnsurePay')
            ->where(['user_id'=>$uid,'state'=>1])
            ->order('id desc')
            ->page($page)
            ->limit(10)
            ->select();
        $this->success('信息返回成功',$list);
    }

    /**
     * 商户充值保证金记录
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function shopEnsurePayList()
    {
        $uid = $this->auth->id;
        $page = input('page/d',1);
        $list = model('app\api\model\service\ShopEnsurePay')
            ->where(['user_id'=>$uid,'state'=>1])
            ->order('id desc')
            ->page($page)
            ->limit(10)
            ->select();
        $this->success('信息返回成功',$list);
    }

    public function ensureLog()
    {
        $uid = $this->auth->id;
        $type = input('type/d',0);
        $page = input('page/d',1);
        $list = \app\api\model\service\EnsureLog::where(['user_id'=>$uid,'type'=>$type])->field('money,memo,createtime')->order('id desc')->page($page)->limit(10)->select();
        $this->success('信息返回成功',$list);
    }

    /**
     * 商家缴纳保证金
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function shopEnsure()
    {
        $uid = $this->auth->id;
        $userInfo = model('app\api\model\service\UserInfo')->where(['user_id'=>$uid])->field('id,is_skill,is_shop')->find();
        $type = input('type','');
        if($type == 'pay')
        {
            $userInfo['is_shop'] != 1 && $this->error('非商家无法支付保证金');
            $id = input('id/d','');
            $paytype = input('paytype/d',0);
            $price = sprintf("%.2f",input('price',''));
            $orderId = 'ShopEn'.Random::alnum(4).'-'.$uid.'-'.time();
            $re = '';
            Db::startTrans();
            try{
                $shopEnsurePay = new \app\api\model\service\ShopEnsurePay(['user_id'=>$uid,'price'=>$price,'orderId'=>$orderId,'paytype'=>$paytype]);
                $shopEnsurePay->allowField(true)->save();
                if($price > 0)
                {
                    $re = \addons\service\library\Pay::payOrder(['amount'=>$price,'orderid'=>$orderId,'title'=>'充值保证金'],$paytype,$uid,2);
                }else{
                    \app\api\model\service\ShopEnsurePay::where(['id'=>$shopEnsurePay->id])->update(['state'=>1,'paytime'=>time()]);
                    \app\api\model\service\ShopEnsurePay::updateShopEnsure($uid,$price);
                }
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error('支付拉起失败',$e->getMessage());
            }
            $this->success('支付信息返回成功',$re);
        }
        $list = model('app\api\model\service\ShopEnsure')->field('id,name,price,accept_nums,service_nums')->order('price asc')->select();
        $this->success('信息返回成功',$list);
    }


}

